var row = 0;
var col = 0;
var rows = 10;
var cols = 10;
var difficulty = 10;
var count = rows * cols;
/* ---------------------------------------------
 * Returns the Cell object for the given row 
 * and column.
 * --------------------------------------------- */
function getCell(r, c) {
	return main.sb.children[0].getChild(r, c);
}

/* ---------------------------------------------
 * Returns the Cell that is currently in focus
 * --------------------------------------------- */
function getCurrent() {
	return main.focus;
}

/* ---------------------------------------------
 * Flags a cell so that it remains covered and will
 * not respond to attempts to uncover it.
 * --------------------------------------------- */
function flag() {
	var cell = getCurrent();
	if (cell.class == 'covered') {
		cell.class='flagged';
	} else if (cell.class == 'flagged') {
		cell.class='covered';
	}
}

/* ---------------------------------------------
 * Triggers the clear cell logic when an onClick
 * occurs.
 * --------------------------------------------- */
function clear(r, c) {
	var cell = getCell(r,c);
	if (cell.class == 'flagged') return;
	if (!cell.covered) return;
	cell.covered = false;
	// count--;
	if (cell.value == "0") {
		uncover(r-1,c-1);
		uncover(r-1,c);
		uncover(r-1,c+1);
		
		uncover(r,c-1);
		uncover(r,c+1);
		
		uncover(r+1,c-1);
		uncover(r+1,c);
		uncover(r+1,c+1);
	} else if (cell.value == "X") {
		showalert('Game Over!', true);
	}
}

function showalert(message, lost) {
	var msgStyle = new Style();
	msgStyle.opacity = "70%";
	if (lost) {
		msgStyle.backgroundColor = "#FF0000";
	} else {
		msgStyle.backgroundColor = "#00FF00";
	}
	
	alertScreen.reset();
	var msgBox = alertScreen.children[0];
	msgBox.style = msgStyle;
	msgBox.value = message;
	show(alertScreen, 1);
	Device.vibrate(100);
	Device.flash();
}

/* ---------------------------------------------
 * Utility function that will optionally call
 * clear() recursively on a cell that has a
 * value of "0".
 * --------------------------------------------- */
function uncover(r, c) {
	if (c < 0 || c >= cols || r < 0 || r >= rows) return;
	cell = getCell(r, c);
	if (!cell.covered) return;
	if (cell.value == "0") {
		clear(r,c);
	} else if (cell.value != "X") {
		cell.covered = false;
	}
}

function genValue() {
	var char = "0";
	if (Math.random(difficulty) == 1) {
		char = "X";
		// One less to clear
		count--;
	}
	return char;
}

/* ---------------------------------------------
 * Prototype Definition for type Cell
 * --------------------------------------------- */
cell.prototype = new TextBox(); 
cell.prototype.onClick= function() {
	clear(row, col);
}; 
 
function cell() {
	this.name = "cell";
	this.halign="center";
	this.valign="center"; 
	this.class="covered"; 
	this.width=15; 
	this.height=15;
	this.covered=true;
	this.focusable=true;
	this.onFocus=function() {
		row = this.row;
		col = this.col;
	};
	this.listen('covered', function() {
		if (this.covered)
			this.class = 'covered';
		else {
			this.class = 'uncovered';
			count--;
			if (count == 0)
				showalert('You Won!', false);
		}
	});
}

function labelCells() {
	/*
	 * Now that the grid is built, iterate over every cell that doesn't 
	 * contain a mine and assign a number that represent how many mines 
	 * surround it.
	 */
	for (var r=0; r < rows; r++) {
		for (var c=0; c < cols; c++) {
			var cell = getCell(r, c);
			if (cell.value != "X") {
				var count = 0;
				if (isMine(r-1, c-1)) {
					count = count + 1;
				} 
				
				if (isMine(r-1, c)) {
					count = count + 1;
				}

				if (isMine(r-1, c+1)) {
					count = count + 1;
				}
				
				if (isMine(r, c+1)) {
					count = count + 1;
				}
				
				if (isMine(r, c-1)) {
					count = count + 1;
				}
				
				if (isMine(r+1, c+1)) {
					count = count + 1;
				}
				
				if (isMine(r+1, c)) {
					count = count + 1;
				}
				
				if (isMine(r+1, c-1)) {
					count = count + 1;
				} 
				
				cell.value = count;
			}
		}
	}
}

function isMine(r, c) {
	if (c < 0 || c >= cols || r < 0 || r >= rows) return false;
	return getCell(r, c).value == "X";
}

